package edu.njfu.sas.dao.impl;



import edu.njfu.sas.dao.UserDao;
import edu.njfu.sas.model.User;
import edu.njfu.sas.util.DBHelper;
import edu.njfu.sas.util.Md5Util;

import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    /**
     * user对象的增删改查
     *
     */
    public boolean checkLogin(User user){
        String sql="select * from tbl_user where username=? and password=? and stuno=?;";
        //把占位符的值放到list中
        List<Object> params=new ArrayList<Object>();
        //一并注入
        params.add(user.getUserName());
        String encryptPassword = Md5Util.stringToMD5(user.getPassword());
        params.add(encryptPassword);
        //创建DBHelper对象
        DBHelper dbHelper = new DBHelper();
        List<Object> query = dbHelper.query(sql, params);
        if(query.isEmpty())
            return false;
        else
            return true;
    }
    /**
     * saveUser(User ser);
     */
    public boolean saveUser(User u){
        String sql="insert into tbl_user(username,password,stuno) values(?,?,?);";
        //对占位符赋值，保存到list中
        List<Object> params = new ArrayList<>();
        params.add(u.getUserName());
        params.add(Md5Util.stringToMD5(u.getPassword()));
        //创建DBHelper对象
        DBHelper db = new DBHelper();
        int result = db.update(sql,params);
        return result>0;
    }
}
